﻿///////////////////////////////////////////////////////////////
//                                                           //
// Code from Arcanea Project.                                //
//                                                           //
// Please include this license part to every reproduction    //
//   of code.                                                //
//                                                           //
// This code is under Ms-RL license                          //
//                                                           //
// Please read futher information about the license,         //
//   the project (news and updates) and the                  //
//   contributors on the address :                           //
//     http://www.codeplex.com/Arcanea/                      //
//                                                           //
///////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using Arcanea.Mediator.XmlClasses;
using System.IO;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace Arcanea.Mediator.Toolbox
{
    /// <summary>
    /// Classe permettant de gerrer les XmlPackets (les interfaces des binpackets)
    /// </summary>
    public class XMLToolbox
    {
        private static XmlSerializer xmls = new XmlSerializer(typeof(List<XmlPacket>));

        /// <summary>
        /// Charge les paquets XML a partir d un fichier
        /// </summary>
        /// <param name="filePath">Path du fichier</param>
        /// <returns>La liste des XmlPackets</returns>
        public static IList<XmlPacket> XmlListPacketLoading(string filePath)
        {
            List<XmlPacket> allPacket = new List<XmlPacket>();
            if (!File.Exists(filePath))
            {
                return allPacket;
            }
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Open);
                allPacket = (List<XmlPacket>)xmls.Deserialize(fs);

            }
            catch (Exception e)
            {
                MessageBox.Show("Impossible de charger le fichier de configuration : " + filePath + "(" + e.Message + ")");
            }
            finally
            {
                if (fs != null) fs.Close();
            }

            return allPacket;
        }

        /// <summary>
        /// Sauvegarde un XmlPacket dans un fichier.
        /// </summary>
        /// <param name="filePath">Path du fichiers</param>
        /// <param name="allPacket">Liste de tous les paquets</param>
        public static void XmlListPacketSaving(string filePath, IList<XmlPacket> allPacket)
        {
            FileStream fs = null;
            try
            {
                fs = new FileStream(filePath, FileMode.Create);
                xmls.Serialize(fs, allPacket);
            }
            catch (Exception e)
            {
                MessageBox.Show("Impossible de sauvegarder le fichier de configuration : " + filePath + "(" + e.Message + ")");
            }
            finally
            {
                if (fs != null) fs.Close();
            }
        }
    }
}
